<?php


namespace campus\controllers;


use common\cache\DictCache;
use common\models\Order;
use common\models\service\OrderRetreatService;
use common\models\service\OrderService;
use Yii;

class OrderController extends BaseController
{
    public function init()
    {
        parent::init();
        $this->model = Order::className();
    }

    /**
     * 列表
     * @return array|string
     */
    public function actionIndex()
    {
        if (Yii::$app->request->isPost) {
            $where = [];
            if ($this->campus_id){
                $where = [['campus_id','=',$this->campus_id]];
            }
            return OrderService::getOperList($where);
        } else {
            $pay_status = DictCache::get('pay_status');
            $order_status = DictCache::get('order_status');
            Yii::$app->view->params['pay_status'] = $pay_status;
            Yii::$app->view->params['order_status'] = $order_status;
            return parent::actionIndex();
        }
    }

    /**
     * 详情
     * @return bool|string
     */
    public function actionDetail()
    {
        $id = Yii::$app->request->get('id', '');
        $result = OrderService::detail(['id'=>$id]);
        return $this->render('',$result);
    }

    /**
     * 发货
     * @return array|mixed
     */
    public function actionDeliver()
    {
        $data = Yii::$app->request->post();
        $order_sn = $data['order_sn']??'';
        return OrderService::deliver($order_sn,3,'','');
    }

    /**
     * 关闭订单
     * @return array
     */
    public function actionCancel()
    {
        $order_sn = Yii::$app->request->post('order_sn', '');
        return OrderService::cancel($order_sn,3,'','');
    }

    /**
     * 修改价格
     * @return array|bool|string
     */
    public function actionEditAmount()
    {
        if (Yii::$app->request->isPost){
            $post = Yii::$app->request->post();
            $pay_amount = $post['pay_amount'];
            $order_sn = $post['order_sn'];
            return OrderService::editAmount($order_sn,$pay_amount,3,'','');
        }
        $order_sn = Yii::$app->request->get('order_sn','');
        $model = Order::find()->where(['order_sn'=>$order_sn])->one();
        if ($model->pay_status!==0){
            return $this->bError('该订单不能修改价格');
        }
        return $this->render('',['info'=>$model]);
    }

    public function actionRefund()
    {
        if (Yii::$app->request->isPost) {
            $where = [];
            if ($this->campus_id){
                $where = [['campus_id','=',$this->campus_id]];
            }
            return OrderRetreatService::getList($where);
        } else {
            $pay_status = DictCache::get('pay_status');
            $order_status = DictCache::get('order_status');
            $review_status = DictCache::get('review_status');
            Yii::$app->view->params['pay_status'] = $pay_status;
            Yii::$app->view->params['order_status'] = $order_status;
            Yii::$app->view->params['review_status'] = $review_status;
            return parent::actionIndex();
        }
    }

    /**
     * 退款审核
     * @return array|bool|string
     */
    public function actionRefundReview()
    {
        if (Yii::$app->request->isPost){
            $post = Yii::$app->request->post();
            $id = $post['id']??'';
            return OrderRetreatService::review($id,$post,3,Yii::$app->user->id);
        }
        $id = Yii::$app->request->get('id','');
        $data = OrderRetreatService::review($id,[],3,Yii::$app->user->id);
        return $this->render('',$data);
    }
}